from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Group
from meiduo_admin.admin_permission.admin_serializers import AdminPermissionSerializers, AdminPermissionSimoleSerializers
from meiduo_admin.my_paginate import MyPageNuberPagination
from users.models import User
from rest_framework.generics import ListAPIView
from rest_framework.response import Response

class AdminPermissionView(ModelViewSet):
    serializer_class = AdminPermissionSerializers
    pagination_class = MyPageNuberPagination
    queryset = User.objects.filter(is_superuser=True,is_staff=True).all()

    #不让它删除自己,重写destroy
    def destroy(self, request, *args, **kwargs):
        now_user_id=request.user.id #获取当前登陆的管理员id
        delete_user=self.get_object()#获取表单传来的对象,通过对象获取要删除的id

        if delete_user==now_user_id:
            return Response("不能删除自身")
        else:
            delete_user.delete()
            return Response(status="200")

#新增管理员用户组权限,这里不能继承ModelViewSet,因为路由不满足
class AdminPermissionSimoleView(ListAPIView):
    serializer_class = AdminPermissionSimoleSerializers
    queryset = Group.objects.all()


